package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.List;

/**
 * 服务逻辑层
 * 1.逻辑代码编写
 * 2.调用数据实现层
 */
public class UserServiceImpl implements IUserService {
    /**
     * 1.判断当前用户名是否存在
     * 2.如果存在，则提示用户重新输入
     * 3.如果不存在，直接新增，提示新增结果
     *
     * @param userModel
     * @return
     */
    @Override
    public String add(UserModel userModel) {
        UserDao userDao = new UserImpl();
//        if条件表示数据库有值，已经存在，则为true，否则为false
        boolean boo = userDao.findByUsername(userModel.getUsername());
        if (boo) {
            return "ishave";
        } else {
            int result = userDao.add(userModel);
            return result == 1 ? "success" : "error";
        }


    }

    /**
     * 1.判断验证码是否证明
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
        if (userModel.getCode().equals(userModel.getSessionCode())) {
            UserDao userDao = new UserImpl();
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            return model == null ? "" : "success";
        } else {
            return "errorcode";
        }

    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.findAll(userModel);
    }

    @Override
    public int del(Integer id) {
        UserDao userDao = new UserImpl();
        return userDao.del(id);
    }

    @Override
    public UserModel findById(Integer id) {
        UserDao userDao = new UserImpl();
        return userDao.findById(id);
    }

    @Override
    public String eidt(UserModel userModel) {
        UserDao userDao = new UserImpl();
//        if条件表示数据库有值，已经存在，则为true，否则为false
        boolean boo = userDao.findByUsername(userModel.getUsername());
        if (boo) {
            return "ishave";
        } else {
            int result = userDao.edit(userModel);
            return result == 1 ? "success" : "error";
        }
    }

    @Override
    public int getCount(UserModel userModel) {
        UserDao userDao=new UserImpl();
        return userDao.getCount(userModel);
    }
}
